/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.dashboard.chart.beans;

import edu.soc.cs5226.gui.dashboard.chart.controller.MemoryAreaBeanController;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.chart.MeterGaugeChartModel;
import javax.faces.event.ActionEvent;

/**
 *
 * @author huijia
 */
@ManagedBean
@SessionScoped
public class MemoryAreaChartBean implements Serializable {
    
    private MemoryAreaBeanController maController;
    private MeterGaugeChartModel meterGaugeModel;
    private String currentValueDisplay;
    private static MemoryAreaChartBean instance = null;
    
    public String getCurrentValueDisplay() {
        return currentValueDisplay;
    }

    public void setCurrentValueDisplay(String currentValueDisplay) {
        this.currentValueDisplay = currentValueDisplay;
    }
    
    public MemoryAreaChartBean() {
        maController = new MemoryAreaBeanController();
        createMeterGaugeModel();
        instance = this;
    }
    
    public static MemoryAreaChartBean getInstance() {
        if (instance==null)
        {
            instance = new MemoryAreaChartBean();
        }
        return instance;
    }

    public MeterGaugeChartModel getMeterGaugeModel() {
        return meterGaugeModel;
    }

    public void setMeterGaugeModel(MeterGaugeChartModel meterGaugeModel) {
        this.meterGaugeModel = meterGaugeModel;
    }

    private void createMeterGaugeModel() {

        // intervals are for color coding of the chart
        /*List<Number> intervals = new ArrayList<Number>(){{
            add(50);    //green 0-50
            add(120);   //yellow 50-120
            add(220);   //red 120-220
        }};*/
        
        List<Number> intervals = maController.getChartIntervals();
        meterGaugeModel = new MeterGaugeChartModel("Performance", getCurrValue(), intervals);
    }
    
    public void displayNewValue() {
        long val = getCurrValue();
        meterGaugeModel.setValue(val);
    }
    
    private long getCurrValue() {
        long currValue = maController.getCurrentValue();
        setCurrentValueDisplay(new Long(currValue).toString());
        return currValue;
    }
    
    public void displayNewIntervals() {
        List<Number> intervals = maController.getChartIntervals();
        meterGaugeModel.setIntervals(intervals);
    }
}
